const mongoUtil = require("./mongo_util")
const collectionName = "student_score"
// const collectionName = "hot"
// 获取热门电视剧
function getScore(value, callback) {
    mongoUtil.getCollection(collectionName).then(collection => {
        // if (value) {
        collection.find({
            SecondaryCollege: value.SecondaryCollege
        }).toArray(function (err, result) { // 返回集合中所有数据
            if (result.length > 0) {
                // console.log("gggggg",result);
                // callback(null, result)
                // new Error()
                const res = result[0].Secondarystudent.filter((item) => {
                    return item.className === value.className
                })[0]
                if (res) {
                    const array = []
                    res.studentList.forEach(element => {
                        const ranking = element.schoolYearList.filter((elem) => {
                            return elem.grade === value.grade
                        })[0]
                        if (ranking && ranking.score_item) {
                            ranking.score_item.filter((ele) => {
                                if (ele.semester === value.semester) {
                                    array.push({
                                        student_Number: element.student_Number,
                                        ComprehensiveSurveyRanking: ele.scoreList.ComprehensiveSurveyRanking,
                                        ComprehensiveSurveyTotalScore: ele.scoreList.ComprehensiveSurveyTotalScore
                                    })
                                }

                                return ele.semester === value.semester
                            })
                        }

                        // console.log("scoreItem===", array);
                        // return [{
                        //     student_Number: element.student_Number,
                        //     ComprehensiveSurveyRanking: ranking
                        // }]

                    });
                    const arr = array.sort((a, b) => {
                        return b.ComprehensiveSurveyTotalScore - a.ComprehensiveSurveyTotalScore
                    })
                    // console.log("level===", arr);
                    const res1 = res.studentList.filter((ite) => {
                        // console.log(ite.student_Number,value.student_Number);
                        return ite.student_Number === value.student_Number
                    })[0]
                    if (res1) {

                        const res2 = res1.schoolYearList.filter((it) => {
                            return it.grade === value.grade
                        })[0]
                        if (res2) {
                            const res3 = res2.score_item.filter((i) => {
                                // console.log("i.semester", i);
                                return i.semester === value.semester
                            })[0]
                            arr.forEach((a, adx) => {
                                if (a.student_Number === res1.student_Number && res3.AdminProcess === '审核通过') {
                                    res3.scoreList.ComprehensiveSurveyRanking = adx + 1
                                }

                            })
                            // console.log("res3==", res3);

                            if (res3) {
                                callback(null, {
                                    grade: res2.grade,
                                    schoolYear: res2.schoolYear,
                                    ...res3
                                })
                                return
                            }
                            callback(err ? err : {
                                reason: "没有数据"
                            }, [])
                        } else {
                            callback(err ? err : {
                                reason: "没有数据"
                            }, [])
                        }
                    } else {
                        callback(err ? err : {
                            reason: "没有数据"
                        }, [])
                    }
                } else {
                    callback(err ? err : {
                        reason: "没有数据"
                    }, [])
                }


            } else {
                // console.log("hhhhhhhh");
                callback(err ? err : {
                    reason: "没有数据"
                }, [])
            }
        });
        // } else {
        //     callback(err ? err : {
        //         reason: "错误传参"
        //     }, null)
        // }

    })
}

module.exports = {
    getScore
}